package com.fox.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fox.config.constant.SystemConstants;
import com.fox.domain.ResponseResult;
import com.fox.domain.entity.Role;
import com.fox.domain.entity.RoleMenu;
import com.fox.domain.entity.UserRole;
import com.fox.domain.vo.PageVo;
import com.fox.mapper.RoleMapper;
import com.fox.service.RoleMenuService;
import com.fox.service.RoleService;
import com.fox.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2024-01-29 16:03:30
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private UserRoleService userRoleService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //判断是否是管理员，如果是返回的集合中只需要有admin
        if (id == 1L) {
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);
    }

    @Override
    public ResponseResult selectRolePage(Role role, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(role.getRoleName()),Role::getRoleName,role.getRoleName());
        queryWrapper.eq(StringUtils.hasText(role.getStatus()),Role::getStatus, SystemConstants.NORMAL);
        queryWrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        //进行vo视图的转换
        List<Role> records = page.getRecords();
        PageVo pageVo = new PageVo(records,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    @Transactional
    public void insertRole(Role role) {
        save(role);
        System.out.println(role.getId());
        //建立角色与菜单的关系
        if (role.getMenuIds() != null && role.getMenuIds().length > 0) {
            insertRoleMenu(role);
        }

    }

    @Override
    @Transactional
    public void updateRole(Role role) {
        updateById(role);
        //删除原先角色和菜单的关联关系
        roleMenuService.deleteRoleMenuByRoleId(role.getId());
        insertRoleMenu(role);
    }

    @Override
    public List<Role> selectRoleAll() {
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus,SystemConstants.STATUS_NORMAL);
        return list(queryWrapper);
    }

    @Override
    public List<Long> selectRoleIdByUserId(Long userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId,userId);
        List<UserRole> list = userRoleService.list(queryWrapper);
        List<Long> result = new ArrayList<>();
        for (UserRole userRole : list) {
            result.add(userRole.getRoleId());
        }
        return result;
    }

    private void insertRoleMenu(Role role) {
        List<RoleMenu> roleMenuList = new ArrayList<>();

        for (Long menuId : role.getMenuIds()) {
            roleMenuList.add(new RoleMenu(role.getId(), menuId));
        }

        roleMenuService.saveBatch(roleMenuList);
    }

}

